package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     *
     * @param dto
     * @return
     */
    @Transactional//因为有增加的操作所以要开启事务
    @Override
    public Result addShoppingCart(ShoppingCartDTO dto) {
        //非空判断
        if (dto == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //判断是否已经存在如果存在则数量加1如果不存在则直接添加到数据库
        //根据用户id判断购物车是否已经存在货品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.query(shoppingCart);
        if (cart != null) {
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
            return Result.success();
        }
        //如果购物车是空的则添加到数据库
        //设置数量和创建时间
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        if (shoppingCart.getSetmealId() != null) {
            Setmeal setmeal = setmealMapper.getBySetmealId(shoppingCart.getSetmealId());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCartMapper.insert(shoppingCart);
            return Result.success();
        }
        DishVO dishVO = dishMapper.getById(shoppingCart.getDishId());
        shoppingCart.setAmount(dishVO.getPrice());
        shoppingCart.setName(dishVO.getName());
        shoppingCart.setImage(dishVO.getImage());
        shoppingCartMapper.insert(shoppingCart);
        return Result.success();
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public Result list() {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getByUserId(BaseContext.getCurrentId());
        return Result.success(shoppingCarts);
    }

    /**
     * 删除购物车中的一个货品
     *
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public Result delete(ShoppingCartDTO dto) {
        if (dto == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.query(shoppingCart);
        if (cart.getNumber() == 1 ) {
            shoppingCartMapper.deleteById(cart.getId());
            return Result.success();
        }
        cart.setNumber(cart.getNumber() - 1);
        shoppingCartMapper.updateNumberById(cart);
        return Result.success();


    }

    @Transactional
    @Override
    public Result clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.clean(shoppingCart);
        return Result.success();
    }
}
